import * as employeeApi from "../../api/employee_manage";
import { SUCCESS_CODE } from "../../config/config";

const UPDATE_PAGENATION = "UPDATE_PAGENATION";
const UPDATE_EMPLOYEE_LIST = "UPDATE_EMPLOYEE_LIST";
const SELECT_EMPLOYEE = "SELECT_EMPLOYEE";
const employeeManage = {
  namespaced: true,
  state: {
    selected: {},
    productCatalogue: [],
    search: {
      productId: "",
      name: "",
      audit: ""
    },
    employeeList: []
  },
  mutations: {
    [UPDATE_EMPLOYEE_LIST](state, employeeList) {
      state.employeeList = employeeList;
    },
    [SELECT_EMPLOYEE](state, employee) {
      state.selected = employee;
    }
  },
  actions: {
    searchEmployee({ commit, state }, search) {
      return new Promise((resolve, reject) => {
        employeeApi
          .searchEmployee({
            limit: search.limit, // 当前页数
            pageNum: search.pageNum, // 每页大小
            status: search.status
          })
          .then(data => {
            data = data.data;
            if (data.status === SUCCESS_CODE) {
              data = data.data;
              let pagination = {
                lastPage: data.lastPage,
                limit: data.limit,
                orderBy: data.orderBy,
                pageCount: data.pageCount,
                pageNum: data.pageNum,
                paging: data.paging,
                total: data.total,
                results: data.results
              };
              commit(UPDATE_EMPLOYEE_LIST, data.results);
              resolve(pagination);
            } else {
              reject(new Error(`getEmployeeList failed! code ${data.status}`));
            }
          });
      });
    },
    selectEmployee({ commit }, employee) {
      commit(SELECT_EMPLOYEE, employee);
    },
    auditEmployee({ commit }, auditObj) {
      return new Promise((resolve, reject) => {
        employeeApi.auditEmployee(auditObj).then(data => {
          console.log(data);
        });
      });
    }
  }
};

export default employeeManage;
